#coding:utf8
import json
import sys
import comm
from transduction import tree_transformer
from classify import analyser

def test_data(results, patterns, prefix=""):
    cnt = [{} for x in range(4)]
    total = {u'全部': len(results)}
    correct_scores = []
    for result, pattern in zip(results, patterns):
        score, result = result
        key = pattern[0][:2]
        total[key] = total.get(key, 0) + 1
        if result[3] == pattern[3]:
            cnt[0][key] = cnt[0].get(key, 0) + 1
        if result[2] == pattern[2]:
            cnt[1][key] = cnt[1].get(key, 0) + 1
        if result == pattern:
            cnt[2][key] = cnt[2].get(key, 0) + 1
            correct_scores.append(score)
        if result[0] == pattern[0]:
            cnt[3][key] = cnt[3].get(key, 0) + 1
    print u'\n\n---------%s 统计结果-------------' % prefix
    for dc in cnt:
        dc[u'全部'] = sum(dc.itervalues())

    print u'疑问词类型匹配比例|\t%s' % (
            reduce(lambda x,y: '%s, %s' % (x, y), 
                [ '%s：%.2f%%' % (k, 100.0 * v / total[k]) for k, v in cnt[1].iteritems() ] ) )
    print u'句型结构匹配比例|\t%s' % (
            reduce(lambda x,y: '%s, %s' % (x, y), 
                [ '%s：%.2f%%' % (k, 100.0 * v / total[k]) for k, v in cnt[0].iteritems() ] ) )
    print u'疑问句类型匹配比例|\t%s' % (
            reduce(lambda x,y: '%s, %s' % (x, y), 
                [ '%s：%.2f%%' % (k, 100.0 * v / total[k]) for k, v in cnt[3].iteritems() ] ) )
    print u'问句类型模式匹配比例|\t%s\t平均分：%.2f' % (
            reduce(lambda x, y: '%s, %s' % (x, y),
                ['%s: %.2f%%' % (k, (100.0 * v / total[k])) for k, v in cnt[2].iteritems() ] ), 1.0*sum(correct_scores)/len(correct_scores) ) 

def tree2str(tree):
    words_grammar = tree.get_grammar()
    sentence = tree.to_sentence()
    words_grammar = analyser._remark_interrog_words(words_grammar, analyser.get_interrog_words(sentence))
    words_grammar = ['%s(%s)' % (word, tag) for word, tag in words_grammar ]
    return ''.join(words_grammar)

def pattern2str(pattern):
    return comm.get_str(pattern)

if __name__ == '__main__':
    from data import ch2rdb

    reload(sys)
    sys.setdefaultencoding('utf8')

    stat_ext = False
    stat_std = True 
    if len(sys.argv) > 1 and sys.argv[1] == 'all':
        stat_std = stat_ext = True
    if len(sys.argv) > 1 and sys.argv[1] == 'ext':
        stat_ext = True
        stat_std = False

    ext_sentences = ch2rdb.get_sentence_data()
    patterns = ch2rdb.load_patterns()

    try:
        comm.load_cache()

        short_sentences = {}
        ext_samples = []
        ext_patterns = []
        for group, pattern in zip(ext_sentences, patterns):
            if stat_std:
                ext_samples.append(group[0][0])
                short_sentences[group[0][0]] = group[0][1]
                ext_patterns.append(pattern)
            if stat_ext:
                for longs, shorts in group[1]:
                    ext_samples.append(longs)
                    short_sentences[longs] = shorts
                    ext_patterns.append(pattern)

        cnt = 1
        results = [[], [], [], []]
        for sample, target_pattern in zip(ext_samples, ext_patterns):
#            if analyser.analyse(sample)[0][1] == target_pattern: continue
            print '---------------SENTENCE %d BEGIN----------------------' % cnt
            cnt += 1
            print 'Source sentence:', sample
            print 'Target sentence:', short_sentences[sample]
            print 'Question type pattern:', pattern2str(target_pattern)
            print 'Interrogative phrase 1:', comm.get_str(analyser.get_interrog_words(sample))
            print '--------Stanford--------'
            short_tree = tree_transformer.transform(comm.raw_get_tree_struct(sample))
            print 'Transformed sentence:', tree2str(short_tree)
            print 'Interrogative phrase 2:', comm.get_str(analyser.get_interrog_words(short_tree.to_sentence()))
            struct_scores = analyser._match_sentence_struct(short_tree.get_grammar(), 3)
            print 'Recommended sentence structures:'
            for score, pattern in struct_scores:
                print '\t(%.2f, %s)' % (score, pattern2str(pattern))
            print 'Recommended question type patterns:'
            pattern_scores = analyser.match_interrog_pattern(short_tree, 3)
            results[0].append(pattern_scores[0])
            for score, pattern in pattern_scores:
                print '\t(%.2f, %s)' % (score, pattern2str(pattern))
            print '--------Formal + Stanford--------'
            print 'Formal  sentence:', comm.get_standard_sentence(sample)
            short_tree = tree_transformer.transform(comm.get_tree_struct(sample))
            print 'Transformed sentence:', tree2str(short_tree)
            print 'Interrogative phrase 2:', comm.get_str(analyser.get_interrog_words(short_tree.to_sentence()))
            struct_scores = analyser._match_sentence_struct(short_tree.get_grammar(), 3)
            print 'Recommended sentence structures:'
            for score, pattern in struct_scores:
                print '\t(%.2f, %s)' % (score, pattern2str(pattern))
            print 'Recommended question type patterns:'
            pattern_scores = analyser.match_interrog_pattern(short_tree, 3)
            results[1].append(pattern_scores[0])
            for score, pattern in pattern_scores:
                print '\t(%.2f, %s)' % (score, pattern2str(pattern))
            print '--------Stanford + Sina-------'
            sentence_tree = comm.raw_get_tree_struct(sample)
            sentence_tree.mark_leaves_num()
            short_tree = tree_transformer.transform(sentence_tree)
            print 'Transformed sentence:', tree2str(short_tree)
            short_sentence = short_tree.to_sentence()
            interrog_words = analyser.get_interrog_words(short_sentence)
            if not interrog_words:
                short_sentence = analyser._sentence_with_interrog_words(sentence_tree, short_tree)
                interrog_words = analyser.get_interrog_words(short_sentence)
            segword_result = json.loads(comm.segword(short_sentence))
            words_grammar = [(x['word'], x['word_std_tag'] or x['word_tag']) for x in segword_result]
            words_grammar = analyser._remark_interrog_words(words_grammar, interrog_words)
            words = [ '%s(%s)' % (word, tag) for word, tag in words_grammar ]
            print 'Transformed sentence 2:', ''.join(words)
            print 'Interrogative phrase 2:', comm.get_str(interrog_words)
            struct_scores = analyser.match_sentence_struct(short_sentence, 3)
            print 'Recommended sentence structures:'
            for score, pattern in struct_scores:
                print '\t(%.2f, %s)' % (score, pattern2str(pattern)), 'Correct!' if pattern == target_pattern else 'Wrong!'
            print 'Recommended question type patterns:'
            pattern_scores = analyser.match_interrog_pattern(short_sentence, 3)
            results[2].append(pattern_scores[0])
            for score, pattern in pattern_scores:
                print '\t(%.2f, %s)' % (score, pattern2str(pattern)), 'Correct!' if pattern == target_pattern else 'Wrong!'
            print '--------Formal + Stanford + Sina-------'
            print 'Formal  sentence:', comm.get_standard_sentence(sample)
            sentence_tree = comm.get_tree_struct(sample)
            sentence_tree.mark_leaves_num()
            short_tree = tree_transformer.transform(sentence_tree)
            print 'Transformed sentence:', tree2str(short_tree)
            short_sentence = short_tree.to_sentence()
            interrog_words = analyser.get_interrog_words(short_sentence)
            if not interrog_words:
                short_sentence = analyser._sentence_with_interrog_words(sentence_tree, short_tree)
                interrog_words = analyser.get_interrog_words(short_sentence)
            segword_result = json.loads(comm.segword(short_sentence))
            words_grammar = [(x['word'], x['word_std_tag'] or x['word_tag']) for x in segword_result]
            words_grammar = analyser._remark_interrog_words(words_grammar, interrog_words)
            words = [ '%s(%s)' % (word, tag) for word, tag in words_grammar ]
            print 'Transformed sentence 2:', ''.join(words)
            print 'Interrogative phrase 2:', comm.get_str(interrog_words)
            struct_scores = analyser.match_sentence_struct(short_sentence, 3)
            print 'Recommended sentence structures:'
            for score, pattern in struct_scores:
                print '\t(%.2f, %s)' % (score, pattern2str(pattern)), 'Correct!' if pattern == target_pattern else 'Wrong!'
            print 'Recommended question type patterns:'
            pattern_scores = analyser.match_interrog_pattern(short_sentence, 3)
            results[3].append(pattern_scores[0])
            for score, pattern in pattern_scores:
                print '\t(%.2f, %s)' % (score, pattern2str(pattern)), 'Correct!' if pattern == target_pattern else 'Wrong!'
    
            print '\n'
    
        test_data(results[0], ext_patterns, "Stanford")
        test_data(results[1], ext_patterns, "Formal + Stanford")
        test_data(results[2], ext_patterns, "Stanford + Sina")
        test_data(results[3], ext_patterns, "Formal + Stanford + Sina")
    
    finally:
        comm.dump_cache()
    
